package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.AskComment;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.AskCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IAskCommentRepository;
import cn.wolfcode.luowowo.comment.service.IAskCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class AskCommentServiceImpl implements IAskCommentService {

    /**
     * spring-data 会自动注入IAskCommentRepository实现类
     *
     * IAskCommentRepository 接口继承MongoRepository
     * 拥有了 分页,排序,crud基本功能
     *    底层实现 : AOP 操作--动态代理
     */
    @Autowired
    private IAskCommentRepository repository;

    @Autowired
    private MongoTemplate template;



    @Override
    public void saveOrUpdate(AskComment askComment) {
        if(!StringUtils.hasLength(askComment.getId())){
            askComment.setCreateTime(new Date());
        }
        repository.save(askComment);
    }

    @Override
    public Page query(AskCommentQuery qo) {
        //repository template 无法直接查询出page分页的数据，所以需要手动封装
        //拼接查询条件
        Query query = new Query();
        if(qo.getQuestionId() != -1){
            query.addCriteria(Criteria.where("questionId").is(qo.getQuestionId()));
        }
        //查总条数
        long total = template.count(query, AskComment.class);
        if(total == 0){
            return Page.empty();
        }
        //查当前页 分页  limit……
        //设置分页、排序信息
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        query.with(pageable);

        List<AskComment> askComments = template.find(query, AskComment.class);
        return new PageImpl(askComments,pageable,total);
    }


    @Override
    public void commentThumbUp(String toid, Long fromid) {
        Optional<AskComment> optionalComment = repository.findById(toid);
        if(optionalComment.isPresent()){
            AskComment comment = optionalComment.get();
            List<Long> thumbuplist = comment.getThumbuplist();
            if(thumbuplist.contains(fromid)){
                //取消点赞
                thumbuplist.remove(fromid);
                comment.setThumbupnum(comment.getThumbupnum()-1);
            }else {
                //点赞
                thumbuplist.add(fromid);
                comment.setThumbupnum(comment.getThumbupnum()+1);
            }
            //更新操作
            this.saveOrUpdate(comment);
        }
    }

    @Override
    public AskComment getThumbupnum(Long id) {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC, "thumbupnum"));
        query.addCriteria(Criteria.where("questionId").is(id));
        List<AskComment> comments = template.find(query, AskComment.class);
        if(comments.size() > 0) {
            return comments.get(0);
        }else {
            return null;
        }
    }

}
